package com.feriaderemates.dao.listing;

import com.feriaderemates.domain.listing.FixedPriceListing;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.support.JdbcDaoSupport;


public class FixedPriceListingDao extends JdbcDaoSupport implements
FixedPriceListingDaoInterface {

	//Queries
	private static String ADD_FIXED_PRICE_LISTING= "INSERT INTO portal.fixed_price_listing " +
			"( accept_returns,return_rules,days_to_process_shipment,price,accept_offers, quantity,requires_immidiate_payment,item_condition) " +
			" VALUES (?,?,?,?,?,?,?,?)";
	
	private static String UPDATE_FIXED_PRICE_LISTING = "UPDATE portal.fixed_price_listing " +
			" accept_returns = ? ,return_rules =?,days_to_process_shipment=?,price=?,accept_offers=?, quantity =? " +
			",requires_immidiate_payment = ?,item_condition =? WHERE listing_id = ?";
	
	private static String GET_FIXED_PRICE_LISTING = "SELECT listing.listing_id,listing.username,listing.description," +
			" listing.number_of_views,listing.warranty,listing.list_status,listing.sale_type," +
			" fixed_price_listing.price,fixed_price_listing.quantity,fixed_price_listing.accept_returns,fixed_price_listing.return_rules " +
			" fixed_price_listing.days_to_process_shipment,fixed_price_listing.item_condition,fixed_price_listing.accept_offers,fixed_price_listing.requires_immediate_payment " +
			"  FROM portal.listing,portal.fixed_price_listing " +
			" WHERE listing.listing_id = fixed_price_listing.listing_id and listing.listing_id =? ";
	
	
	@Override
	public void addFixedPriceListing(final FixedPriceListing listing) {
		getJdbcTemplate().update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(ADD_FIXED_PRICE_LISTING);
                ps.setBoolean(1, listing.isAcceptingReturns());
                ps.setString(2, listing.getReturnRules());
                ps.setInt(3, listing.getDaysToProcessShipment());
                ps.setBigDecimal(4, listing.getPrice());
                ps.setBoolean(5, listing.isAcceptingOffers());
                ps.setInt(6, listing.getQuantity());
                ps.setBoolean(7, listing.isRequiresImmediatePayment());
                ps.setString(8, listing.getItemCondition().name());
                
                return ps;
            }
        });
      }

	@Override
	public FixedPriceListing getFixedPriceListingById(long listingId) {
            return (FixedPriceListing) getJdbcTemplate().queryForObject(GET_FIXED_PRICE_LISTING, new Object[]{listingId},new FixedPriceListingRowMapper());
	}

	@Override
	public void updateFixedPriceListing(final FixedPriceListing listing) {
		getJdbcTemplate().update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(UPDATE_FIXED_PRICE_LISTING);
                ps.setBoolean(1, listing.isAcceptingReturns());
                ps.setString(2, listing.getReturnRules());
                ps.setInt(3, listing.getDaysToProcessShipment());
                ps.setBigDecimal(4, listing.getPrice());
                ps.setBoolean(5, listing.isAcceptingOffers());
                ps.setInt(6, listing.getQuantity());
                ps.setBoolean(7, listing.isRequiresImmediatePayment());
                ps.setString(8, listing.getItemCondition().name());
                ps.setLong(9, listing.getListingId());
                return ps;
            }
        });
	}

	
}
